#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/15 18:00
# @Author : 闫峰
# @File    : view.py
import datetime
import json
from urllib import parse
import random
from flask import request, Response
from flask_service.service.charge.v1_apis import ChargeApis
from util import handler_request_log
from util.otherutils.functions_control import  get_uuid
from util.mongodb_utils import MongoDBLink
from util.redis_control.redis_operations import RedisOperator
user = parse.quote_plus("viviu")
passwd = parse.quote_plus("viu@1234")
modb_client = MongoDBLink(
    uri=f'mongodb://{user}:{passwd}@10.86.3.113:27017/?authSource=admin',
    dbname='sit_charge_mock_data', coll_name='sit_charge_mock_data')
redis_client = RedisOperator()


def start_game():
    is_forward = True

    # 使用示例
    # value = redis_client.get_key(path)
    # if value == "1":
    #     is_forward = False
    sit_base_url = "http://cg-sit-auto.yuntiancloud.com/charge-manager-union" + path
    request_method = request.method
    if request_method in ["DELETE","GET"]:
        request_json = None
    else:
        request_json = request.json
    resp_json = None
    response = None
    if is_forward:
        status_code,resp_json = ChargeApis.charge_common_handler(request_method, sit_base_url, request_json)
        response = Response(json.dumps(resp_json), status=200, mimetype='application/json')
    else:
        status_code = random.choice([500])
        match status_code:
            case 500:
                resp_json = {
                    "ret": {
                        "code": status_code,
                        "reason": f"{path}调用失败",
                        "msg": f"{path}调用失败",
                        "request_id": str(get_uuid()),
                    }
                }
                resp_str = json.dumps(resp_json)
                response = Response(resp_str, status=status_code, mimetype='application/json')
    handler_request_log(request,status_code,resp_json,modb_client,True)
    return response
